home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / util / sgihack.c < prev   
C/C++ Source or Header  |  1992-10-12  |  2KB  |  70 lines

  1. /* Nuke the symbols _end, _etext, and _edata in an a.out file's symbol
  2.  * table.  A bug in SGI's linker causes incremental loading of object
  3.  * files to fail if the base a.out contains one of these linker-defined
  4.  * symbols.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <a.out.h>
  9. #include <malloc.h>
  10.  
  11. char *badsyms[] = { "_end", "_etext", "_edata", 0 };
  12.  
  13. main(ac, av) char **av; {
  14.     FILE *f;
  15.     struct filehdr filehdr;
  16.     HDRR symhdr;
  17.     char **pp, *p, *strings;
  18.     long max;
  19.     int gotone = 0;
  20.  
  21.     if (ac != 2) {
  22.     printf("Usage: %s a.out-file\n", ac == 0 ? "sgihack" : av[0]);
  23.     return 1;
  24.     }
  25.     if ((f = fopen(av[1], "r+")) == 0) {
  26.     printf("Cannot open %s\n", av[1]);
  27.     return 1;
  28.     }
  29.     if (fread((char *)&filehdr, sizeof(filehdr), 1, f) != 1) {
  30.     printf("Cannot read filehdr\n");
  31.     return 1;
  32.     }
  33.     (void)fseek(f, filehdr.f_symptr, SEEK_SET);
  34.     if (fread((char *)&symhdr, sizeof(symhdr), 1, f) != 1) {
  35.     printf("Cannot read symhdr\n");
  36.     return 1;
  37.     }
  38.     max = symhdr.issExtMax;
  39.     if ((strings = malloc(max)) == 0) {
  40.     printf("Cannot malloc %ld bytes\n", max);
  41.     return 1;
  42.     }
  43.     (void)fseek(f, symhdr.cbSsExtOffset, SEEK_SET);
  44.     if (fread(strings, 1, max, f) != max) {
  45.     printf("Cannot read string table\n");
  46.     return 1;
  47.     }
  48.     for (p = strings; p < strings+max; ) {
  49.     for (pp = badsyms; *pp; pp++) {
  50.         if (strcmp(p, *pp) == 0) {
  51.         gotone++;
  52.         printf("Nuking symbol %s\n", p);
  53.         p[0] = '?';
  54.         }
  55.     }
  56.     while (*p != '\0') p++;
  57.     while (p < strings+max && *p == '\0') p++;
  58.     }
  59.     if (!gotone) {
  60.     printf("No bad symbols found in %s\n", av[1]);
  61.     return 0;
  62.     }
  63.     (void)fseek(f, symhdr.cbSsExtOffset, SEEK_SET);
  64.     if (fwrite(strings, 1, max, f) != max) {
  65.     printf("Cannot write back string table\n");
  66.     return 1;
  67.     }
  68.     return 0;
  69. }
  70.